<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$length = 50;
$limit_page = $page;
$limit_length = $length;
$limit_start = ($limit_page - 1) * $limit_length;

//
if( $request_link_subclass == '' ) {
	$request_link_current = 'admin/' . $request_link_class . '/';
} else {
	$request_link_current = 'admin/' . $request_link_class . '/' . $request_link_subclass . '/';
}
$injectObj->addb('func_get_tpl_pagelayout_content', 'context_wrap', 'URL_LINK', $request_link_current);

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.admin.class.php');
$feObj = new FrontendAdminBase();

//
if( $request_link_class == 'login' || $request_link_class == 'logout' ) {
	//
} elseif( $_SESSION['suser']->userid == 0 || $_SESSION['suser']->usertype != 'admin' ) {
	$feObj->redirect('admin/login/?goto=' . $request_type . '/' . $request_link);
}

//log
if( $action != '' ) {
	$userObj->addLog($action);
}

//cache
if( $action != '' ) {
	$tpl->tsUpdate();
}

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.validate.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'common.validate.class.php');
$vObj = new CommonValidate();

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.upload.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.file.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'attachment.function.php');
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'thumb.function.php');

//
//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.resource.category.class.php');
//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.resource.class.php');
//$rescObj = new CoreResourceCategoryBase();
//$resObj = new CoreResourceBase();
//include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'attachment.function.php');
//

//call
//common attach
if( $action != '' ) {
	//timestamp
	$timestamptfile = CONFIG_PATH . CONFIG_DIR_CACHE . 'timestamp.txt';
	$fp = fopen($timestamptfile, 'w');
	fwrite($fp, time());
	fclose($fp);
	//
	$attachments_ids = array();
	if( isset($_FILES['attachments_files']) ) {
		$attachmentfileObj = new CoreMultiUpload('attachments_files');
		$attachmentfileObj->setDestination(CONFIG_PATH . CONFIG_DIR_RESOURCES);
		if( $attachmentfileObj->parse() && $attachmentfileObj->save() ) {
			$attachments_savefilenames = $attachmentfileObj->save_filename;
			//$attachments_savefiles = $attachmentfileObj->save_file;
			$attachments_filenames = $attachmentfileObj->filename;
			$attachments_filetypes = $attachmentfileObj->filetype;
			$attachments_filesizes = $attachmentfileObj->filesize;
			//
			if( is_array($attachments_savefilenames) ) {
				for($i=0, $n=sizeof($attachments_savefilenames); $i<$n; $i++) {
					if( $attachments_savefilenames[$i] ) {
						$data_array = array(
									'resources_filename' => $attachments_filenames[$i],
									'resources_filetype' => $attachments_filetypes[$i],
									'resources_filesize' => $attachments_filesizes[$i],
									'resources_value' => $attachments_savefilenames[$i],
									'resources_name' => $attachments_filenames[$i],
									'resources_description' => $_POST['attachments_desc'][$i],
									'resources_status' => '1',
										);
						$resources_id = $resObj->addItem($data_array);
					} else {
						$resources_id = '';
					}
					
					$attachments_ids[] = $resources_id;
				}
			}
		}
		//filter
		if( isset($_POST['field_introtext']) ) {
			$_POST['field_introtext'] = func_attachment_post_convert($_POST['field_introtext'], $attachments_ids);
		}
		if( isset($_POST['field_fulltext']) ) {
			$_POST['field_fulltext'] = func_attachment_post_convert($_POST['field_fulltext'], $attachments_ids);
		}
		if( isset($_POST['field_description']) ) {
			$_POST['field_description'] = func_attachment_post_convert($_POST['field_description'], $attachments_ids);
		}
	}
	//filter
	//$srhurl = "/src\=([\"\/\\\.])images\/uploads\//";
	//$repurl = '"' . CONFIG_URL_HTTP . 'images/uploads/';
	//
	$srhurl = array("'<img(.+?)src=([\\\\])(.+?)images\/uploads\/(.+?)>'si", "'<img(.+?)src=(.+?)images\/uploads\/(.+?)>'si");
	$repurl = array("<img$1src=$2\"" . CONFIG_URL_HTTP . "images/uploads/$4>", "<img$1src=\"" . CONFIG_URL_HTTP . "images/uploads/$3>");
	if( isset($_POST['field_introtext']) ) {
		$_POST['field_introtext'] = preg_replace($srhurl, $repurl, $_POST['field_introtext']);
	}
	if( isset($_POST['field_fulltext']) ) {
		$_POST['field_fulltext'] = preg_replace($srhurl, $repurl, $_POST['field_fulltext']);
	}
	if( isset($_POST['field_description']) ) {
		$_POST['field_description'] = preg_replace($srhurl, $repurl, $_POST['field_description']);
	}
}

//link
if( $request_link_class ) {
	switch($request_link_class) {
	//---------------------------------------------------------------------------//
	//---------------------------------------------------------------------------//
		default:
			if( util_file_exists(CONFIG_PATH . CONFIG_DIR_FRONTS . 'admin/', $request_link_class, '.admin.front.php') ) {
				include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'admin/' . $request_link_class . '.admin.front.php');
			} else {
				include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'admin/' . 'home.admin.front.php');
			}
			break;
	}
} else {
	include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'admin/' . 'home.admin.front.php');
}
//
?>